package it.polimi.swim.servlet;

import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.HelpRequestManagerBean;
import it.polimi.swim.session.HelpRequestManagerBeanRemote;
import it.polimi.swim.session.SkillManagerBean;
import it.polimi.swim.session.SkillManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HelpRequestAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public HelpRequestAnswerServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("post");
			
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			System.out.println("Chiamo il sesison");
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup(HelpRequestManagerBean.REMOTE);
			HelpRequestManagerBeanRemote helpRequestManager = (HelpRequestManagerBeanRemote) ref;
			Object ref2 = ctx.lookup(SkillManagerBean.REMOTE);
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref2;

			
			// TODO RECUPERA INFORMAZIONI HELPREQUEST
			int idHelp = Integer.parseInt(request.getParameter("idHelp"));
			if(request.getParameter("ans").compareTo("Deny")==0){
				helpRequestManager.deleteHelp(idHelp);
			}
			request.getRequestDispatcher("homeUser.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("post");
			
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			System.out.println("Chiamo il sesison");
			InitialContext ctx = new InitialContext();
			Object ref = ctx.lookup(HelpRequestManagerBean.REMOTE);
			HelpRequestManagerBeanRemote helpRequestManager = (HelpRequestManagerBeanRemote) ref;
			Object ref2 = ctx.lookup(SkillManagerBean.REMOTE);
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref2;

			
			// TODO RECUPERA INFORMAZIONI HELPREQUEST
			//int ans = Integer.parseInt(request.getParameter("ans"));
			//System.out.println("ans "+ans);
			//int idHelp = Integer.parseInt((String) request.getAttribute("idHelp"));
			int idHelp = Integer.parseInt(request.getParameter("idHelp"));
			if(request.getParameter("ans").compareTo("Deny")==0){
				helpRequestManager.deleteHelp(idHelp);
			}
			else{
				helpRequestManager.confirmHelp(idHelp);
			}
			request.getRequestDispatcher("homeUser.jsp").forward(request, response);

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
